#include <QXmlStreamAttribute>

#include "backend.h"

void Backend :: fromBackendMessageCommand(QByteArray Data){
//<Robot>
    //<Info Type="6R" Model="ZK6" Order="1"/>
    //<RIstF X="0" Y="0" Z="0" A="0" B="0" C="0"/>
    //<RIstW X="0" Y="0" Z="0" A="0" B="0" C="0"/>
    //<AIpos A1="0" A2="0" A3="0" A4="0" A5="0" A6="0"/>
    //<EIPos E1="0" E2="0"/>
    //<Com D1="0" D2="0" D3="0" D4="0" D5="0" D6="0" D7="0" D8="0"/>
//<Robot>

    int loopi = 0;                                                                              //定义迭代递增参数loopi
    QXmlStreamReader mQXmlStreamReader;                                                         //定义一个Xml流读取器
    mQXmlStreamReader.addData(Data);                                                           //将收到的 来自机器人的数据 加入到Xml流读取器中

    while(!mQXmlStreamReader.atEnd()){                                                          //没有读取到末尾时，执行循环，否则跳出
        if(mQXmlStreamReader.isStartElement()){                                                 //如果读取到 元素开始
            if(mQXmlStreamReader.name() == "Info"){

            }
            else if(mQXmlStreamReader.name() == "RIstF"){                                            //判断 该元素名 是不是 "AIPos"
                loopi = 0;                                                                      //重新给loopi赋值为0
                foreach (const QXmlStreamAttribute & attribute, mQXmlStreamReader.attributes()){//遍历 该元素 的属性
                    QStringRef tempStringRef;                                                   //定义QStringRef类型数据，用来接收属性值
                    QString  tempQString;                                                       //定义QString类型数据  用来接收QStringRef类型转为QString的数据
                    tempStringRef = attribute.value();                                          //tempStringRef接收属性值
                    tempQString = tempStringRef.toString();                                     //tempQString接收Qstring类型的属性值
                    this->pvisualRobot->mEndPointData.actCarPos2Foot[loopi]= tempQString.toDouble();
                    loopi++;                                                                    //记录下一个属性值
                }
            }

            else if(mQXmlStreamReader.name() == "RIstW"){                                       //再判断元素名是不是 "AITor"
                loopi = 0;                                                                      //重新给loopi赋值为0
                foreach (const QXmlStreamAttribute & attribute, mQXmlStreamReader.attributes()){//遍历 该元素 的属性
                    QStringRef tempStringRef;                                                   //定义QStringRef类型数据，用来接收属性值
                    QString  tempQString;                                                       //定义QString类型数据  用来接收QStringRef类型转为QString的数据
                    tempStringRef = attribute.value();                                          //tempStringRef接收属性值
                    tempQString = tempStringRef.toString();                                     //tempQString接收Qstring类型的属性值
                    this->pvisualRobot->mEndPointData.actCarPos[loopi]= tempQString.toDouble();
                    loopi++;                                                                    //记录下一个属性值
                }
            }

            else if(mQXmlStreamReader.name() == "AIPos"){                                       //再判断元素名是不是 "AITor"
                loopi = 0;                                                                      //重新给loopi赋值为0
                foreach (const QXmlStreamAttribute & attribute, mQXmlStreamReader.attributes()){//遍历 该元素 的属性
                    QStringRef tempStringRef;                                                   //定义QStringRef类型数据，用来接收属性值
                    QString  tempQString;                                                       //定义QString类型数据  用来接收QStringRef类型转为QString的数据
                    tempStringRef = attribute.value();                                          //tempStringRef接收属性值
                    tempQString = tempStringRef.toString();                                     //tempQString接收Qstring类型的属性值
                    this->pvisualRobot->mJointData[loopi].actJointPos= tempQString.toDouble();
                    loopi++;                                                                    //记录下一个属性值
                }
            }
            else if(mQXmlStreamReader.name() == "EIPos"){                                       //再判断元素名是不是 "AITor"

            }
            else if(mQXmlStreamReader.name() == "Com"){                                       //再判断元素名是不是 "AITor"

            }
        }
        mQXmlStreamReader.readNext();                                               //读取下一个
    }
    mQXmlStreamReader.clear();                                                      //清空Xml读取器中的数据

}

void Backend :: toBackendMessage(QByteArray& Data){
        //char* tempSend = new char[64];
        char tempSend[64];
        QString SendString;
        QXmlStreamWriter stream(&SendString);

        memset(tempSend,'\0',strlen(tempSend));
        stream.writeStartElement("Hmi");

            stream.writeEmptyElement("SystemBtn");
                sprintf(tempSend,"%d", this->stSystemBtn.EStop);
                stream.writeAttribute("EStop",tempSend);
                sprintf(tempSend,"%d", this->stSystemBtn.vPower);
                stream.writeAttribute("vPower",tempSend);
                sprintf(tempSend,"%d", this->stSystemBtn.Pause);
                stream.writeAttribute("Pause",tempSend);
                sprintf(tempSend,"%d", this->stSystemBtn.Run);
                stream.writeAttribute("Run",tempSend);
                sprintf(tempSend,"%d", this->stSystemBtn.Stop);
                stream.writeAttribute("Stop",tempSend);

            stream.writeEmptyElement("DirectionBtn");
                sprintf(tempSend,"%d", this->stDirectionBtn.XN);
                stream.writeAttribute("XN",tempSend);
                sprintf(tempSend,"%d", this->stDirectionBtn.XP);
                stream.writeAttribute("XP",tempSend);
                sprintf(tempSend,"%d", this->stDirectionBtn.YN);
                stream.writeAttribute("YN",tempSend);
                sprintf(tempSend,"%d", this->stDirectionBtn.YP);
                stream.writeAttribute("YP",tempSend);
                sprintf(tempSend,"%d", this->stDirectionBtn.ZN);
                stream.writeAttribute("ZN",tempSend);
                sprintf(tempSend,"%d", this->stDirectionBtn.ZP);
                stream.writeAttribute("ZP",tempSend);
                sprintf(tempSend,"%d", this->stDirectionBtn.AN);
                stream.writeAttribute("AN",tempSend);
                sprintf(tempSend,"%d", this->stDirectionBtn.AP);
                stream.writeAttribute("AP",tempSend);
                sprintf(tempSend,"%d", this->stDirectionBtn.BN);
                stream.writeAttribute("BN",tempSend);
                sprintf(tempSend,"%d", this->stDirectionBtn.BP);
                stream.writeAttribute("BP",tempSend);
                sprintf(tempSend,"%d", this->stDirectionBtn.CN);
                stream.writeAttribute("CN",tempSend);
                sprintf(tempSend,"%d", this->stDirectionBtn.CP);
                stream.writeAttribute("CP",tempSend);
                sprintf(tempSend,"%d", this->stDirectionBtn.E1N);
                stream.writeAttribute("E1N",tempSend);
                sprintf(tempSend,"%d", this->stDirectionBtn.E1P);
                stream.writeAttribute("E1P",tempSend);
                sprintf(tempSend,"%d", this->stDirectionBtn.E2N);
                stream.writeAttribute("E2N",tempSend);
                sprintf(tempSend,"%d", this->stDirectionBtn.E2P);
                stream.writeAttribute("E2P",tempSend);

            stream.writeEmptyElement("FnBtn");
                sprintf(tempSend,"%d", this->stFnBtn.Fn1);
                stream.writeAttribute("Fn1", tempSend);
                sprintf(tempSend,"%d", this->stFnBtn.Fn2);
                stream.writeAttribute("Fn2", tempSend);
                sprintf(tempSend,"%d", this->stFnBtn.Fn3);
                stream.writeAttribute("Fn3", tempSend);
                sprintf(tempSend,"%d", this->stFnBtn.Fn4);
                stream.writeAttribute("Fn4", tempSend);

            stream.writeEmptyElement("OperationInfo");
                sprintf(tempSend,"%d", this->stOperationInfo.OperationMode);
                stream.writeAttribute("OperationMode", tempSend);
                sprintf(tempSend,"%d", this->stOperationInfo.MotionCoordinate);
                stream.writeAttribute("MotionCoordinate", tempSend);
                sprintf(tempSend,"%d", this->stOperationInfo.ToolNumber);
                stream.writeAttribute("ToolNumber", tempSend);
                sprintf(tempSend,"%d", this->stOperationInfo.WorkNumber);
                stream.writeAttribute("WorkNumber", tempSend);
                sprintf(tempSend,"%.1f", this->stOperationInfo.MannualOvn);
                stream.writeAttribute("MannualOvn", tempSend);
                sprintf(tempSend,"%.1f", this->stOperationInfo.ProgramOvn);
                stream.writeAttribute("ProgramOvn", tempSend);

        stream.writeEndElement();
        stream.writeEndDocument();

        Data = SendString.toLocal8Bit();

        //delete []tempSend;
}
